package com.jh3502.vivo.function.todo;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.function.tododetail.reim.TodoReimActivity;
import com.jh3502.vivo.function.tododetail.travel.TodoTravelActivity;
import com.jh3502.vivo.utils.Constant;
import com.jh3502.vivo.utils.Utils;
import com.jh3502.vivo.view.MyRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author:harisucici on 2018/11/7 10:42
 * email:harisucici@gmail.com
 * name:
 * todo:
 */
public class TodoListActivity extends BaseActivity<TodoListPresenter.TodoListView, TodoListPresenter> implements TodoListPresenter.TodoListView {
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.todo_list_recycler)
    MyRecyclerView todoListRecycler;
    List<Map<String, Object>> bodyList = new ArrayList<>(2);
    private TodoListAdapter mAdapter;
    private List<TodoListBean> dataList = new ArrayList<>(2);

    @Override
    public TodoListPresenter initPresenter() {
        return new TodoListPresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_todo;
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        toolbarTitle.setText("待办");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mAdapter = new TodoListAdapter(this, dataList);
        mAdapter.setOnItemClickListener(i -> {
            Bundle bundle = new Bundle();
            bundle.putInt("id", dataList.get(i).getId());
            bundle.putString("businessNo", dataList.get(i).getBusinessNo());
            bundle.putInt("source", Constant.TRAVEL_APPROVE);
            if (dataList.get(i).getType() == 0) {
                Utils.getInstance().startActivity(this, TodoTravelActivity.class, bundle);
            } else if (dataList.get(i).getType() == 1) {
                Utils.getInstance().startActivity(this, TodoReimActivity.class, bundle);
            }
        });
        mAdapter.setButtonSetOnclickConfirm(position -> {
            bodyList.clear();
            bodyList.add(doApprove(1, dataList.get(position)));
            mPresenter.toAgreeForAll(bodyList);
        });
        mAdapter.setButtonSetOnclickReject(position -> {
            bodyList.clear();
            bodyList.add(doApprove(2, dataList.get(position)));
            mPresenter.toAgreeForAll(bodyList);
        });

        todoListRecycler.setAdapter(mAdapter);
        mPresenter.setTodoListView(this);

    }

    private Map<String, Object> doApprove(int state, TodoListBean todoListBean) {
        Map<String, Object> body = new HashMap<>(2);
        body.put("userId", todoListBean.getUserId());
        body.put("type", todoListBean.getType());
        body.put("state", state);
        body.put("destination", todoListBean.getDestination());
        body.put("businessNo", todoListBean.getBusinessNo());
        body.put("id", todoListBean.getId());
        body.put("flowNo", todoListBean.getFlowNo());
        body.put("currentNodeNo", todoListBean.getCurrentNodeNo());
        return body;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_all, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case R.id.action_all:
                bodyList.clear();
                for (TodoListBean todoListBean : dataList) {
                    bodyList.add(doApprove(1, todoListBean));
                    mPresenter.toAgreeForAll(bodyList);
                }
                break;
            default:
        }
        return true;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {
        mPresenter.toFindTodo();

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {
        if (code == Constant.LIST_EMPTY) {
            dataList.clear();
            mAdapter.notifyDataSetChanged();
        }
    }


    @Override
    public void findTodoSuccess(List<TodoListBean> datas) {
        dataList.clear();
        dataList.addAll(datas);
        mAdapter.notifyDataSetChanged();

    }

    @Override
    public void agreeForAllSuccess() {
        showShortToast("审批成功");
        mPresenter.toFindTodo();
    }
}
